<?php

require_once('classes/modeles/Individus/Personne.php');
require_once('classes/modeles/Individus/Pompier.php');
require_once('classes/modeles/Individus/User.php');

/**
 * Description of Individus
 *
 * @author ROCU Fabien
 */
class Individus {

    public $array_personne_id;
    public $array_personne;

    private $array_user_id;
    private $array_user;

    public $array_pompier_id;
    public $array_pompier;

    function __construct() {

    }

    /**************************************************************************/
    /************************* PERSONNE ***************************************/
    /**************************************************************************/

    public function charger_arrayPersonneID_individus() {
        global $_mysql;

        $tmp = array();

        try {
            $query = "SELECT personne_id FROM personne";
            if ( Config::$debugSQL ) echo $query;
            $result = $_mysql->query($query);

            if ( $_mysql->num_rows($result) > 0 ) {
                for ($i = 0 ; $row = $_mysql->fetch_array($result) ; $i++) {
                    $tmp[$i] = $row['personne_id'];

                }

                $this->array_personne_id = $tmp;
            }
            else
                throw new Exception('Erreur Individus: la création de la liste des identifiants de personne a échoué');

        }
        catch (SQLException $sqle) {
            throw $sqle;
        }
    }

    public function charger_arrayPersonne_individus() {
        $tmp = array();

        for ($i = 0 ; $i < count($this->array_personne_id) ; $i++) {
            $personne = new Personne();
            $personne->charger_personne($this->array_personne_id[$i]);

            $tmp[$this->array_personne_id[$i]] = serialize($personne);
        }

        $this->array_personne = $tmp;
    }

    public function creer_personne( $nom, $prenom, $adresse, $cp, $ville, $tel, $port, $email ) {
        $personne = new Personne();
        $personne->set_personne($nom, $prenom, $adresse, $cp, $ville, $tel, $port, $email);
        $id = $personne->creer_personne();

        $nb_personne = count($this->array_personne_id);
        $this->array_personne_id[$nb_personne] = $id;

        $this->array_personne[$id] = serialize($personne);
    }

    public function sauvegarder_personne( $id, $nom, $prenom, $adresse, $cp, $ville, $tel, $port, $email ) {
        $personne = unserialize($this->array_personne[$id]);
        $personne->set_personne($nom, $prenom, $adresse, $cp, $ville, $tel, $port, $email);
        $personne->sauvegarder_personne();

        $this->array_personne[$id] = serialize($personne);
    }

    public function effacer_personne( $id ) {
        $personne = new Personne();
        $personne->effacer_personne($id);

        unset($this->array_personne[$id]);

        $this->charger_arrayPersonneID_individus();
    }

    /**************************************************************************/
    /***************************** USER ***************************************/
    /**************************************************************************/









    /**************************************************************************/
    /************************** POMPIER ***************************************/
    /**************************************************************************/

    public function charger_arrayPompierID_individus() {
        global $_mysql;

        $tmp = array();

        try {
            $query = "SELECT pompier_id FROM pompier";
            if ( Config::$debugSQL ) echo $query;
            $result = $_mysql->query($query);

            if ( $_mysql->num_rows($result) > 0 ) {
                for ($i = 0 ; $row = $_mysql->fetch_array($result) ; $i++) {
                    $tmp[$i] = $row['pompier_id'];

                }

                $this->array_pompier_id = $tmp;
            }
            else
                throw new Exception('Erreur Individus: la création de la liste des identifiants de pompier a échoué');

        }
        catch (SQLException $sqle) {
            throw $sqle;
        }
    }

    public function charger_arrayPompier_individus() {
        $tmp = array();

        for ($i = 0 ; $i < count($this->array_pompier_id) ; $i++) {
            $pompier = new Pompier();
            $pompier->charger_pompier($this->array_pompier_id[$i]);

            $tmp[$this->array_pompier_id[$i]] = serialize($pompier);
        }

        $this->array_pompier = $tmp;
    }

    public function creer_pompier( $personne_id, $matricule, $grade_id, $type, $mutualisation ) {
        $pompier = new Pompier();
        $pompier->set_pompier($personne_id, $matricule, $grade_id, $type, $mutualisation);
        $id = $pompier->creer_pompier();

        $nb_pompier = count($this->array_pompier_id);
        $this->array_pompier_id[$nb_pompier] = $id;

        $this->array_pompier[$id] = serialize($pompier);
    }

    public function sauvegarder_pompier( $id, $personne_id, $matricule, $grade_id, $type, $mutualisation ) {
        $pompier = unserialize($this->array_pompier[$id]);
        $pompier->set_pompier($personne_id, $matricule, $grade_id, $type, $mutualisation);
        $pompier->sauvegarder_pompier();

        $this->array_pompier[$id] = serialize($pompier);
    }

    public function effacer_pompier( $id ) {
        $pompier = new Pompier();
        $pompier->effacer_pompier($id);

        unset($this->array_pompier[$id]);

        $this->charger_arrayPompierID_individus();
    }

    public function getPompierID_with_Matricule( $matricule ) {
        for ( $i = 0 ; $i < count($this->array_pompier_id) ; $i++ ) {
            $pompier = unserialize($this->array_pompier[$this->array_pompier_id[$i]]);
            if ( $pompier->pompier_matricule == $matricule )
                    return $pompier->pompier_id;
        }
    }

    public function getFreePersonne() {
        $pompier = new Pompier();
        return $pompier->getFreePersonne();
    }
}
?>
